/*
并发执行任务
@param { Function[] } tasks
@param { Number } paralleCount 同时可执行的任务数
*/
function paralleTask(tasks, paralleCount = 2) {
    return new Promise((resolve, reject) => {
        if (tasks.length===0) {
            resolve();
            return;
        }
        let nextTaskIndex = 0;
        let finishTaskIndex = 0; // 已完成任务数
        function _run() {
            // 运行下一个任务
            const task = tasks[nextTaskIndex];
            nextTaskIndex++;
            task().then(() => {
                finishTaskIndex++;
                if (nextTaskIndex < tasks.length) {
                    // 运行下一个任务
                    _run();
                } else if (finishTaskIndex === tasks.length) {
                    // 任务完成
                    resolve();
                }
            })
        }
        for (let i = 0; i < paralleCount && i < tasks.length; i++) {
            _run();
        }
    })
}
   